<template>
  <el-container>
    <el-header>
      <!--  顶部 -->
      <div>
        <img src="@/assets/logo.png"
             alt="系统logo"
             width="60px"
             height="50px">
        <span>电商后台管理平台</span>
      </div>
      <el-button type="success"
                 round
                 size="small"
                 @click="logout">安全退出</el-button>
    </el-header>
    <!-- 中间布局 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '60px' : '160px' ">
        <el-radio-group v-model="isCollapse"
                        style="margin-bottom: 20px;">
          <el-radio-button :label="false">展开</el-radio-button>
          <el-radio-button :label="true">收起</el-radio-button>
        </el-radio-group>
        <el-menu :default-active="activeItem"
                 background-color="#373d41"
                 text-color="#fff"
                 :unique-opened="true"
                 :collapse="isCollapse"
                 :router="true"
                 :collapse-transition="false">
          <!-- 具有子导航的导航按钮 -->
          <el-submenu :index="menu.path"
                      v-for="menu in menuList"
                      :key="menu.id">
            <!-- 一级导航 -->
            <template slot="title">
              <i :class="IcionsObj[menu.id]"></i>
              <span slot="title">{{menu.authName}}</span>
            </template>
            <!-- 二级导航 -->
            <el-menu-item :index="'/'+subMenu.path"
                          v-for="subMenu in menu.children"
                          :key="subMenu.id"
                          @click="saveActiveItem('/'+subMenu.path)">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span slot="title">{{subMenu.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 设置是否默认收起
      isCollapse: false,
      menuList: [],
      IcionsObj:
      {
        125: 'el-icon-s-custom', // 用户管理
        103: 'el-icon-key', // 权限管理
        101: 'el-icon-goods', // 商品管理
        102: 'el-icon-document', // 订单管理
        145: 'el-icon-pie-chart'// 数据统计
      },
      activeItem: ''
    }
  },
  // 数据的请求时机 使用钩子函数 created
  async created () {
    const { data: res } = await this.$http.get('/menus')
    if (res.meta.status !== 200) {
      this.$msg.error(res.meta.msg)
      return
    }
    this.menuList = res.data
    this.activeItem = this.$http.sessionStorage.getItem('activeItem')
    if (!this.activeItem) {
      this.activeItem = '/users'
    }
  },
  methods: {
    logout () {
      // 清除token
      window.sessionStorage.clear()
      // 返回登录页
      this.$router.push('/login')
    },
    saveActiveItem (item) {
      this.$http.sessionStorage.setItem('activeItem', item)
    }
  }
}
</script>
<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #373d41;
  .el-menu {
    background-color: #373d41;
  }
}
</style>
